<template>
	<view class="contain">
		<view class="fixed">
			<cu-custom :isBack="false" :color="color" bgColor="bg-shadeTop" @goBack="goBack">
				<block slot="content"><view class="content">{{lang.file.parameterfile}}</view></block>
			</cu-custom>
		</view>
		<uni-swipe-action class="list">
			<uni-swipe-action-item
				v-for="(v, k) in list"
				:key="k"
				:right-options="options"
				@change="change($event, v.id)"
				@click="userOptions"
			>
				<view class="list-item" @tap="goDetail(v)">{{ v.title }}</view>
			</uni-swipe-action-item>
		</uni-swipe-action>
		<mix-modal ref="mixModal" :title="lang.file.prompt" :text="lang.file.suredelete" @onConfirm="onConfirm"></mix-modal>
	</view>
</template>

<script>
import {mapState, mapMutations} from "vuex";
import Toast from "../../common/toast.js";
const bassUrl = require("@/common/config.js").bass;
import uniSwipeAction from "@/components/uni-swipe-action/uni-swipe-action.vue";
import uniSwipeActionItem from "@/components/uni-swipe-action-item/uni-swipe-action-item.vue";
export default {
	components: {
		uniSwipeAction,
		uniSwipeActionItem
	},
	computed: {
		...mapState(["lang"])
	},
	data() {
		return {
			souce: 0,
			color: "#333",
			list: [],
			id: "",
			dataList: [],
			options: [
				{
					text: "分享",
					style: {
						backgroundColor: "#007aff"
					}
				},
				{
					text: "删除",
					style: {
						backgroundColor: "#dd524d"
					}
				}
			]
		};
	},
	onLoad(options) {
		this.dataList = this.$store.state.paramList;
		this.souce = options.souce;
		this.init(this.dataList);
		this.options[0].text=this.lang.file.share;
		this.options[1].text=this.lang.file.delete;
	},
	methods: {
		change(e, id) {
			this.id = id;
		},
		share(href) {
			let _this = this;
			uni.showActionSheet({
				//itemList: ["微信"],
				itemList: [_this.lang.file.wechat],
				success(res) {
					if (res.tapIndex == 0) {
						_this.wxShare(href);
					} else {
						_this.QQShare(href);
					}
				}
			});
		},
		wxShare(href) {
			uni.share({
				provider: "weixin",
				scene: "WXSceneSession",
				//title: "英搏尔蓝牙",
				title: this.lang.file.enpowerbluetooth,
				href: href,
				success: function(res) {
					console.log("success:" + JSON.stringify(res));
				},
				fail: function(err) {
					console.log("fail:" + JSON.stringify(err));
				}
			});
		},
		QQShare(href) {
			uni.share({
				provider: "qq",
				//title: "英搏尔蓝牙",
				title: this.lang.file.enpowerbluetooth,
				href: href,
				success: function(res) {
					console.log("success:" + JSON.stringify(res));
				},
				fail: function(err) {
					console.log("fail:" + JSON.stringify(err));
				}
			});
		},
		onConfirm() {
			uni.showLoading({
				//title: "加载中..."
				title: this.lang.file.loading,
			});
			// /authc/api/user/delete_text_by_id
			this.$u.get("authc/api/user/delete_text_by_id", {id: this.id}).then(res => {
				console.log(res);
				uni.hideLoading();
				if (res.code == 0) {
					this.init(this.dataList);
				}
			});
		},
		userOptions(e) {
			if (e.index == 0) {
				this.share(`${bassUrl}/bm/authc/api/user/download?id=${this.id}`);
			} else {
				this.$refs.mixModal.open();
			}
		},
		init(name) {
			uni.showLoading({
				//title: "加载中..."
				title: this.lang.file.loading,
			});
			let data1 = {
				identification: name
			};
			this.$u.post("authc/api/user/find_txt_list", data1).then(res => {
				console.log(res);
				uni.hideLoading();
				if (res.code == 0) {
					this.list = res.data;
				}
			});
		},
		goBack() {
			uni.navigateBack({
				delta: 1
			});
		},
		/**
		 * 获取页面栈
		 * @param {Number} preIndex为1时获取上一页
		 * @return {Object} 
		 */
		prePage(preIndex = 1){
			const pages = getCurrentPages();
			const prePage = pages[pages.length - (preIndex + 1)];
		
			return prePage.$vm;
		},
		goDetail(item) {
			if (this.souce == 1) {
				this.$u
					.get("authc/api/user/find_txt_id", {
						id: item.id
					})
					.then(res => {
						console.log(res);
						if (res.code == 0) {
							this.$store.commit("setChangeList", res.data);
							//Toast.showToast("选择成功");
							Toast.showToast(this.lang.file.choosesucc);
							// setTimeout(() => {
								this.prePage().goLoad();
								uni.navigateBack({
									delta: 1
								});
							// }, 500);
						}
					});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.contain {
	width: 100%;
	background: #f1f1f1;
	display: flex;
	flex-direction: column;
	align-items: center;

	.fixed {
		width: 100%;

		.content {
			width: 100%;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
	}

	.list {
		width: 100%;
		padding: 0 28rpx;
		box-sizing: border-box;
		background: #f1f1f1;
		margin-top: 30rpx;

		.list-item {
			width: 100%;
			padding: 0 24rpx;
			// box-sizing: border-box;
			height: 100upx;
			line-height: 100upx;
			background: #ffffff;
			// border-radius: 6px;
			font-size: 28rpx;
			color: #333;
			margin-bottom: 30rpx;
		}
	}
}
</style>
